using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Common;
using Common.NotifyEvent;

namespace SProject.BasicInformation
{
	[Serializable]
	public class Target : NotifyPropertyChanged
	{
		private string _ChannelSetting;

		private bool _IsReadOnly;

		private ObservableCollection<TargetItem> _Items = new ObservableCollection<TargetItem>();

		public string Channels
		{
			get
			{
				return _ChannelSetting;
			}
			private set
			{
				_ChannelSetting = value;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return _IsReadOnly;
			}
			set
			{
				if (_IsReadOnly != value)
				{
					_IsReadOnly = value;
					OnPropertyChanged("IsReadOnly");
				}
			}
		}

		public ObservableCollection<TargetItem> Items
		{
			get
			{
				return _Items;
			}
		}

		public Target()
		{
		}

		public Target(Target target)
		{
			Copy(target);
		}

		public void UpdateChannels()
		{
			if (string.IsNullOrEmpty(Channels))
			{
				Channels = ConfigReader.GetInstance().GetChannels();
			}
		}

		public void Copy(Target target)
		{
			Items.Clear();
			_IsReadOnly = target._IsReadOnly;
			for (int i = 0; i < target.Items.Count; i++)
			{
				TargetItem targetItem = new TargetItem();
				targetItem.Copy(target.Items[i]);
				Items.Add(targetItem);
			}
		}

		public int GetTubeCount()
		{
			int num = 0;
			foreach (TargetItem item in Items)
			{
				num = ((num > item.TubeNo) ? num : item.TubeNo);
			}
			return num;
		}

		public bool Equals(Target target)
		{
			if (target == null)
			{
				return false;
			}
			if (base.Equals(target))
			{
				return true;
			}
			if (IsReadOnly != target.IsReadOnly || Items.Count != target.Items.Count)
			{
				return false;
			}
			for (int i = 0; i < Items.Count; i++)
			{
				if (!Items[i].Equals(target.Items[i]))
				{
					return false;
				}
			}
			return true;
		}

		public void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_ProjectTarget);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			StreamEx streamEx = new StreamEx();
			streamEx.Write(BytesConverter.GetBytes(Items.Count));
			foreach (TargetItem item in Items)
			{
				item.SaveToStream(streamEx);
			}
			list.Add(PropertyKey.Key_Items);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			list.Add(PropertyKey.Key_IsReadOnly);
			list2.Add(BytesConverter.GetBytes(IsReadOnly));
			if (!string.IsNullOrEmpty(Channels))
			{
				list.Add(PropertyKey.Key_ChannelSetting);
				list2.Add(BytesConverter.GetBytes(Channels));
			}
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public bool ReadFromStream(StreamEx stream)
		{
			Items.Clear();
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_ProjectTarget)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					for (int i = 0; i < keys.Count; i++)
					{
						switch (keys[i])
						{
						case PropertyKey.Key_Items:
						{
							StreamEx streamEx = new StreamEx();
							streamEx.Write(dates[i]);
							streamEx.Seek(0L, SeekOrigin.Begin);
							int num = streamEx.ReadInt();
							for (int k = 0; k < num; k++)
							{
								TargetItem targetItem = new TargetItem();
								targetItem.ReadFromStream(streamEx);
								Items.Add(targetItem);
							}
							streamEx.Close();
							break;
						}
						case PropertyKey.Key_IsReadOnly:
							IsReadOnly = BytesConverter.BytesToBool(dates[i]);
							break;
						case PropertyKey.Key_ChannelSetting:
							Channels = BytesConverter.BytesToString(dates[i]);
							if (Channels.IndexOf('&') < 0)
							{
								string text = string.Empty;
								for (int j = 0; j < Channels.Length; j++)
								{
									text = ((j + 1 != Channels.Length) ? (text + Channels[j] + '&'.ToString()) : (text + Channels[j]));
								}
								Channels = text;
							}
							break;
						}
					}
					return true;
				}
				stream.Seek(-4L, SeekOrigin.Current);
				int num2 = stream.ReadInt();
				for (int l = 0; l < num2; l++)
				{
					TargetItem targetItem2 = new TargetItem();
					targetItem2.ReadFromStream(stream);
					Items.Add(targetItem2);
				}
				return true;
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
